<?php

/*
 * Copyright 2015 jiaojie <jiaojie1989@gmail.com>.
 * 
 * Check your synatx if it is right.
 * Check your variables if the names are just correct.
 * Finally, run it under your command line.
 * Enjoy it!
 */

namespace Ziroom\Util\Redis;

use Redis;

/**
 * Redis Operations
 * @author jiaojie <jiaojie1989@gmail.com>
 * @date 2015-4-15
 * @version 1.0.0
 * @description
 */
class Client {

    /**
     * Redis Instance
     * @var Redis 
     */
    protected $redis;

    /**
     * Singleton of Client
     * @var Ziroom\Util\Redis\Client 
     */
    protected static $client;

    protected function __construct() {
        $this->redis = new Redis();
        $this->redis->connect('172.16.5.113', 6379);
    }

    public static function getInstance() {
        if (!self::$client instanceof self) {
            self::$client = new self;
        }
        return static::$client;
    }

    public function __call($name, $arguments) {
        
    }

    /*   通用方法   */

    public function keys($pattern = 'MSIGN::*') {
        if($pattern == '*') {
            return false;
        }
        return $this->redis->keys($pattern);
    }
    
    public function del($key = null) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->delete($key);
    }

    public function setTimeout($key = null, $timeout = 86400) {
        if (!isset($key) || !is_numeric($timeout) || $timeout <= 0) {
            return false;
        }
        return $this->redis->setTimeout($key, $timeout);
    }
    
    public function isExist($key = null) {
        if(!isset($key)) {
            return false;
        }
        return $this->redis->exists($key);
    }
    
    /**
     * get TTL
     * @param string $key
     * @return integer
     * <p>
     * -1 stand for none expiration
     * any other integer stand for seconds to live
     * -2 stand for none existence
     * </p>
     */
    public function ttl($key = null) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->ttl($key);
    }
    
    /**
     * Persist TTL
     * @param string $key
     * @return integer 
     * <p>
     * 0 stand for failure
     * 1 stand for success
     * </p>
     */
    public function persistTtl($key = null) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->persist($key);
    }

    /*   String类型操作   */

    public function get($key = null) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->get($key);
    }

    public function set($key = null, $value = 1, $timeout = 0, $force = false) {
        if (!isset($key) || !is_numeric($timeout) || $timeout < 0) {
            return false;
        } else {
            if (0 == $timeout) {
                if ($force) {
                    return $this->redis->set($key, $value);
                } else {
                    return $this->redis->setnx($key, $value);
                }
            } else {
                if ($force) {
                    return $this->redis->setex($key, $timeout, $value);
                } else {
                    if ($this->redis->setnx($key, $value)) {
                        return $this->setTimeout($key, $timeout);
                    }
                    return false;
                }
            }
        }
    }

    /*   Set类型操作   */

    public function sAdd($key = null, $value = 1) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->sAdd($key, $value);
    }

    public function sCard($key = null) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->scard($key);
    }

    public function sIsMemeber($key = null, $value) {
        if (!isset($key) || !isset($value)) {
            return false;
        }
        return $this->redis->sismember($key, $value);
    }

    public function sMembers($key) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->sMembers($key);
    }

    public function sRandMember($key = null) {
        if (!isset($key)) {
            return false;
        }
        return $this->redis->sRandMember($key);
    }

    public function sRemove($key = null, $value) {
        if (!isset($key) || !isset($value)) {
            return false;
        }
        return $this->redis->sRemove($key, $value);
    }

}
